$('document').ready(function () {
    // 加载页面时触发的事件
    if (parseInt(getCookie('code')) !== 203) {
        window.location.href = "home.html"
    }
    // 获取教师列表
    var totalPageNum = 0
    $.ajax({
        type: 'GET',
        url: 'http://111.229.120.67/sch/teachers',
        data: {
            "pageNum": 1,
            "pageSize": 30
        },
        xhrFields: {
            withCredentials: true
        },
        success: function (data) {
            if (data.statusCode == 0) {
                console.log(data)
                totalPageNum = data.page.totalPageNum
                if (totalPageNum > 1) {
                    for (var i = 2; i < totalPageNum + 1; i++) {
                        $('#raquo').before(`
                        <li id="page` + i + `"><a href="javascript:void(0)">` + i + `</a></li>
                        `)
                    }
                } else {
                    $('#pagination').css('display', 'none')
                }
                var array = data.page.list
                for (var i = 0; i < array.length; i++) {
                    $('#list-tbody').append(`
                        <tr>
                            <td>` + array[i].id + `</td>
                            <td>` + array[i].realname + `</td>
                            <td>` + array[i].profession + `</td>
                            <td>
                                
                                <a href="javascript:void(0)" class="teacher-upade">修改</a>
                                <a href="javascript:void(0)" class="teacher-remove">删除</a>                
                            </td>
                        </tr>
                    `)
                }
            } else {
                $('#myModal-public').modal({})
                $('#myModal-public h4').html('请求超时')
                $('#myModal-public-body').html('请求超时，请重试')
            }
        }
    })
    // 分页
    $('#pagination').on('mouseover', function () {
        $('#pagination li').off('click').click(function () {
            $('#pagination li').removeClass("active")
            $('#pagination li').eq($(this).text()).addClass("active")
            // console.log($(this).text())
            // console.log($('#pagination .active').text())
            if ($('#pagination .active').text() == '1') {
                $('#laquo').css('display', 'none')
            } else {
                $('#laquo').css('display', 'inline')
            }
            if ($('#pagination .active').text() == totalPageNum) {
                $('#raquo').css('display', 'none')
            } else {
                $('#raquo').css('display', 'inline')
            }
            $.ajax({
                type: 'GET',
                url: 'http://111.229.120.67/sch/teachers',
                data: {
                    "pageNum": parseInt($(this).text()),
                    "pageSize": 30
                },
                xhrFields: {
                    withCredentials: true
                },
                success: function (data) {
                    // console.log(data)
                    if (data.statusCode == 0) {
                        $('#list-tbody').html('')
                        var array = data.page.list
                        for (var i = 0; i < array.length; i++) {
                            $('#list-tbody').append(`
                                    <tr>
                                    <td>` + array[i].id + `</td>
                                    <td>` + array[i].realname + `</td>
                                    <td>` + array[i].profession + `</td>
                                    <td>
                                        
                                        <a href="javascript:void(0)" class="teacher-upade">修改</a>
                                        <a href="javascript:void(0)" class="teacher-remove">删除</a>                
                                    </td>
                                    </tr>
                                `)
                        }
                    } else {
                        $('#myModal-public').modal({})
                        $('#myModal-public h4').html('请求超时')
                        $('#myModal-public-body').html('请求超时，请重试')
                    }
                }
            })
        })
        $('#laquo').off('click').click(function () {
            var active = $('#pagination .active').text()
            var laquo = $('#pagination li').eq(active - 1).text()
            $('#pagination .active').removeClass("active")
            $('#pagination li').eq(laquo).addClass("active")
            if (laquo == 1) {
                $('#laquo').css('display', 'none')
            }
            if ($('#pagination .active').text() !== totalPageNum) {
                $('#raquo').css('display', 'inline')
            }
            $.ajax({
                type: 'GET',
                url: 'http://111.229.120.67/sch/teachers',
                data: {
                    "pageNum": parseInt($('#pagination li').eq(laquo).text()),
                    "pageSize": 30
                },
                xhrFields: {
                    withCredentials: true
                },
                success: function (data) {
                    if (data.statusCode == 0) {
                        $('#list-tbody').html('')
                        var array = data.page.list
                        for (var i = 0; i < array.length; i++) {
                            $('#list-tbody').append(`
                            <tr>
                            <td>` + array[i].id + `</td>
                            <td>` + array[i].realname + `</td>
                            <td>` + array[i].profession + `</td>
                            <td>
                                
                                <a href="javascript:void(0)" class="teacher-upade">修改</a>
                                <a href="javascript:void(0)" class="teacher-remove">删除</a>                
                            </td>
                            </tr>
                        `)
                        }
                    } else {
                        $('#myModal-public').modal({})
                        $('#myModal-public h4').html('请求超时')
                        $('#myModal-public-body').html('请求超时，请重试')
                    }
                }
            })
        })
        $('#raquo').off('click').click(function () {
            var active = $('#pagination .active').text()
            var raquo = $('#pagination li').eq(parseInt(active) + 1).text()
            $('#pagination .active').removeClass("active")
            $('#pagination li').eq(raquo).addClass("active")
            $('#laquo').css('display', 'inline')
            if ($('#pagination .active').text() == totalPageNum) {
                $('#raquo').css('display', 'none')
            }
            $.ajax({
                type: 'GET',
                url: 'http://111.229.120.67/sch/teachers',
                data: {
                    "pageNum": parseInt($('#pagination li').eq(raquo).text()),
                    "pageSize": 30
                },
                xhrFields: {
                    withCredentials: true
                },
                success: function (data) {
                    console.log(data)
                    if (data.statusCode == 0) {
                        $('#list-tbody').html('')
                        var array = data.page.list
                        for (var i = 0; i < array.length; i++) {
                            $('#list-tbody').append(`
                            <tr>
                            <td>` + array[i].id + `</td>
                            <td>` + array[i].realname + `</td>
                            <td>` + array[i].profession + `</td>
                            <td>
                                
                                <a href="javascript:void(0)" class="teacher-upade">修改</a>
                                <a href="javascript:void(0)" class="teacher-remove">删除</a>                
                            </td>
                            </tr>
                        `)
                    }
                    } else {
                        $('#myModal-public').modal({})
                        $('#myModal-public h4').html('请求超时')
                        $('#myModal-public-body').html('请求超时，请重试')
                    }
                }
            })
        })
    })
    // -------------------------------------------
    var teacher_id=0  //旧教师ID全局变量
    $('#list-tbody').on('mouseover',function(){
        // 修改教师信息
        $('.teacher-upade').off('click').click(function () {
            var upade_index = $('.teacher-upade').index($(this)[0])
            var teacherid = $('#table-responsive tbody tr:eq(' + upade_index + ') td').eq(0).html()
            var teachername = $('#table-responsive tbody tr:eq(' + upade_index + ') td').eq(1).html()
            var teacherpro = $('#table-responsive tbody tr:eq(' + upade_index + ') td').eq(2).html()
            teacher_id=teacherid
            $('#myModal-public h4').html('修改教师信息(ID:' + teacherid + ')')
            $('#myModal-public-body').html(`
                账号：<input type="text" id="newid" value="` + teacherid + `" placeholder="不填写表示该项保留原状">
                <br>
                <br>
                密码：<input type="text" id="password" placeholder="不填写表示该项保留原状">
                <br>
                <br>
                姓名：<input type="text" id="realname" value="` + teachername + `" placeholder="不填写表示该项保留原状">
                <br>
                <br>
                职称：<input type="text" id="profession" value="` + teacherpro + `" placeholder="不填写表示该项保留原状">
            `)
            $('#myModal-public .modal-footer').html(
                `<button type="button" id="surebtn" class="btn btn-primary">确认修改
        </button><button type="button" class="btn" data-dismiss="modal">关闭</button>`
            )
            $('#myModal-public').modal({
    
            })
        })
        // 删除教师
        $('.teacher-remove').off('click').click(function () {
            var remove_index = $('.teacher-remove').index($(this)[0])
            var teacherid = $('#table-responsive tbody tr:eq(' + remove_index + ') td').eq(0).html()
            var teachername = $('#table-responsive tbody tr:eq(' + remove_index + ') td').eq(1).html()
            var teacherpro = $('#table-responsive tbody tr:eq(' + remove_index + ') td').eq(2).html()
            teacher_id=teacherid
            $('#myModal-public h4').html('删除ID(' + teacherid + ')教师')
            $('#myModal-public-body').html(`
                姓名:` + teachername + `
                <br>
                <br>
                职称:` + teacherpro + `
            `)
            $('#myModal-public .modal-footer').html(`
            <button type="button" id="sureremove" class="btn btn-primary">确认删除</button>
            </button><button type="button" class="btn" data-dismiss="modal">关闭</button>
            `)
            $('#myModal-public').modal({
    
            })
        })
    })
    
    // 修改，删除模态框
    $('#myModal-public').on('mouseenter', function () {
        // 修改的确认按钮
        $('#surebtn').off('click').click(function () {
            $.ajax({
                type: 'POST',
                url: 'http://111.229.120.67/sch/set-teac',
                data: {
                    "id": teacher_id,
                    "newid":$('#newid').val(),
                    "realname":$('#realname').val(),
                    "password":$('#password').val(),
                    "profession":$('#profession').val()
                },
                xhrFields: {
                    withCredentials: true
                },
                success: function (data) {
                    if (data.statusCode == 0) {
                        $('#myModal-public-body').html('修改成功')
                        $('#myModal-public .modal-footer').html('<button type="button" class="btn" data-dismiss="modal">关闭</button>')
                        setTimeout(function(){
                            window.location.href="teacher-list.html"
                        },1000)
                    } else {
                        $('#myModal-public h4').html('请求超时')
                        $('#myModal-public-body').html('请求超时，请重试')
                    }
                }
            })
        })
        // 删除的确定按钮
        $('#sureremove').off('click').click(function () {
            $.ajax({
                type: 'POST',
                url: 'http://111.229.120.67/sch/del-teac',
                data: {
                    "id": teacher_id,
                },
                xhrFields: {
                    withCredentials: true
                },
                success: function (data) {
                    if (data.statusCode == 0) {
                        $('#myModal-public-body').html('删除成功')
                        $('#myModal-public .modal-footer').html('<button type="button" class="btn" data-dismiss="modal">关闭</button>')
                        setTimeout(function(){
                            window.location.href="teacher-list.html"
                        },1000)
                    } else {
                        $('#myModal-public h4').html('请求超时')
                        $('#myModal-public-body').html('请求超时，请重试')
                    }
                }
            })
            // ajax方法：
            // $.post('教师删除接口url',teacherid, function (data, status, xhr) {
            //     console.log(data)
            //     console.log(data.statusCode)
            //     if (data.statusCode == 0) {
            //         $('#myModal-public-body').html('删除成功')
            //         $('#myModal-public .modal-footer').html('<button type="button" class="btn" data-dismiss="modal">关闭</button>')
            //         $('#myModal-public').modal({
            //         })
            //     }else{
            //         $('#myModal-public .modal-footer').html('删除失败(服务器故障)')
            //     }
            // })
            
        })
    })

   
    // --------------------------
    // 模态框
    $('#myModal .modal-footer button').eq(1).click(function () {
        window.location.href = "login.html"
    })
})